package handler

import (
	"awesomeProject/app/model"
	"awesomeProject/app/request"
	"awesomeProject/app/response"
	"awesomeProject/app/service"
	"awesomeProject/app/verify"
	"github.com/gin-gonic/gin"
	"strconv"
)

type HostHandler struct{}

var (
	hostSrv service.HostSrv
)

// Pagination 主机列表（分页）
func (r *HostHandler) Pagination(ctx *gin.Context) {
	// 表单验证
	var req request.HostPagination
	_ = ctx.Bind(&req)
	if err := verify.HostPagination(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	// 操作人
	response.Result(ctx, hostSrv.Pagination(req))
	return
}

// Create 主机创建
func (r *HostHandler) Create(ctx *gin.Context) {
	// 表单校验
	var req request.HostCreate
	_ = ctx.Bind(&req)
	if err := verify.HostCreate(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	// 校验店铺原密码与店铺加密密码是否匹配
	accountHashPwd, err := accountSrv.GetHashPwd(req.AccountOriginPwd)
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	if accountHashPwd != req.AccountPassword {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("店铺登录密码与店铺加密密码不匹配"))
		return
	}
	// 校验店铺账号是否已被其他主机绑定
	if checkBind := hostSrv.CheckAccountUsernameBind(req.AccountUsername, 0); checkBind {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("该店铺账号已被绑定"))
		return
	}
	// 校验店铺账号是否存在
	if err, account := accountSrv.GetAccount(req.AccountUsername); account.ID == 0 || err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("该店铺账号不存在"))
		return
	}
	// 校验主机IP记录是否已存在 (允许多个店铺账号绑定同一个主机IP， 顾下面屏蔽)
	//if checkExists := hostSrv.CheckHostIpExists(req.HostIP, 0); checkExists {
	//	response.Result(ctx, response.ErrInvalidParam.SetMsg("该主机IP记录已经存在！"))
	//	return
	//}
	hostFillAble := model.HostFillAble{
		HostIP:        req.HostIP,
		HostPort:      req.HostPort,
		HostUsername:  req.HostUsername,
		HostPassword:  req.HostPassword,
		ReleaseStatus: req.ReleaseStatus,
		ReleaseDate:   req.ReleaseDate,
		Issues:        req.Issues,
		Status:        req.Status,
	}
	hostAcountFillAble := model.HostAccountFillAble{
		AccountUsername:  req.AccountUsername,
		AccountPassword:  req.AccountPassword,
		AccountOriginPwd: req.AccountOriginPwd,
	}
	err, account := hostSrv.GetHostAccountByAccountUsername(req.AccountUsername)
	if err == nil && (req.AccountPassword == "" || req.AccountOriginPwd == "") {
		hostAcountFillAble.AccountUsername = account.Username
		hostAcountFillAble.AccountPassword = account.Password
		hostAcountFillAble.AccountOriginPwd = account.OriginPwd
	}
	response.Result(ctx, hostSrv.Create(hostFillAble, hostAcountFillAble))
	return
}

// Show 主机详情
func (r HostHandler) Show(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	err, account := hostSrv.FindByID(uint(id))
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	response.Result(ctx, response.OK.WithData(account))
	return
}

// Update 主机更新
func (r *HostHandler) Update(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	var req request.HostUpdate
	_ = ctx.ShouldBindJSON(&req)
	if err := verify.HostUpdate(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	// 校验店铺原密码与店铺加密密码是否匹配
	accountHashPwd, err := accountSrv.GetHashPwd(req.AccountOriginPwd)
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	if accountHashPwd != req.AccountPassword {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("店铺登录密码与店铺加密密码不匹配"))
		return
	}
	// 校验店铺账号是否已被其他主机绑定
	if checkBind := hostSrv.CheckAccountUsernameBind(req.AccountUsername, req.ID); checkBind {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("该店铺账号已被绑定"))
		return
	}
	// 校验店铺账号是否存在
	if err, account := accountSrv.GetAccount(req.AccountUsername); account.ID == 0 || err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("该店铺账号不存在"))
		return
	}
	// 校验主机IP记录是否已存在 (允许多个店铺账号绑定同一个主机IP， 顾下面屏蔽)
	//if checkExists := hostSrv.CheckHostIpExists(req.HostIP, req.ID); checkExists {
	//	response.Result(ctx, response.ErrInvalidParam.SetMsg("该主机IP记录已经存在！"))
	//	return
	//}
	response.Result(ctx, hostSrv.Update(uint(id), req))
	return
}

// Del 删除
func (r *HostHandler) Del(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	response.Result(ctx, hostSrv.Del(uint(id)))
	return
}

// 主机详细状态
func (r HostHandler) HostStatesPagination(ctx *gin.Context) {
	// 表单验证
	var req request.HostStatesPagination
	_ = ctx.Bind(&req)
	if err := verify.HostStatesPagination(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	// 分页列表数据
	response.Result(ctx, hostSrv.HostStatesPagination(req))
	return
}

// HostReboot 主机重启
func (r *HostHandler) HostReboot(ctx *gin.Context) {
	var req request.HostReboot
	_ = ctx.ShouldBindJSON(&req)
	if err := verify.HostReboot(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	response.Result(ctx, hostSrv.HostReboot(req))
	return
}